package com.lwl.blog.dao.impl;

import com.lwl.blog.dao.UserDao;
import com.lwl.blog.entity.UserEntity;
import com.lwl.blog.utils.SystemUserUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 帖子用户dao层实现类
 */
public class UserDaoImpl implements UserDao {
    //创建连接池接口
    QueryRunner qr = new QueryRunner(SystemUserUtil.ds);

    //添加
    @Override
    public Integer insert(UserEntity u) throws SQLException {
        //Integer id, String username, String password, String salt, String email, Integer type,
        // Integer status, String activationCode, String headerUrl, Date createTime
        String sql= "insert into t_user(username,password,email,type,status,activation_code,create_time)  values(?,?,?,?,?,?,?)";
        return qr.update(sql,u.getUsername(),u.getPassword(),u.getEmail(),u.getType(),u.getStatus(),u.getActivationCode(),u.getCreateTime());

    }


    //编辑信息
    @Override
    public Integer updateById(UserEntity u) throws SQLException {
        String sql = "update t_user set username = ?,email = ?,status = ? where id = ?";
        return qr.update(sql,u.getUsername(),u.getEmail(),u.getStatus(),u.getId());
    }

    //根据id删除
    @Override
    public Integer deleteById(Integer id) throws SQLException {
        return null;
    }

    //查询所有
    @Override
    public List<UserEntity> selectList() throws SQLException {
        return null;
    }

    //根据id查询
    @Override
    public UserEntity selectById(Integer id) throws SQLException {
        return null;
    }

    //批量删除
    @Override
    public Integer batchRemove(List<Integer> ids) throws SQLException {
        return null;
    }

    @Override
    public UserEntity selByUname(String username) throws SQLException {
        //1.准备sql
        String sql = "select * from t_user where username=?";

        return qr.query(sql, new BeanHandler<UserEntity>(UserEntity.class), username);

    }

    @Override
    public UserEntity selectActCode(String code) throws SQLException {
        String sql = "select *,activation_code as activationCode from t_user where activation_code=?";
        System.out.println(code);
        return qr.query(sql,new BeanHandler<UserEntity>(UserEntity.class),code);
    }


}
